package com.neo.common.base;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;

public class ReturnJson {

    private static final Logger log = LoggerFactory.getLogger(ReturnJson.class);


    public static String jsonString(String message, Object data, int status) {
        Map<String, Object> returnMap = new LinkedHashMap();
        returnMap.put("status", status);
        returnMap.put("data", data == null ? new Object() : data);
        returnMap.put("message", message);
        returnMap.put("timestamp", System.currentTimeMillis() / 1000);
        JSONObject json = getJsonObject(returnMap);
        String st = json.toString();
        if (StringUtils.isNotBlank(st) && st.length() > 550) {
            log.info("=======result===========:" + st.substring(0, 550));
        } else {
            log.info("=======result===========:" + st);
        }
        return st;
    }

    private static JSONObject getJsonObject(Map<String, Object> returnMap) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerDefaultValueProcessor(Double.class, new DefaultDefaultValueProcessor() {
            public Object getDefaultValue(Class type) {
                return null;
            }
        });

        return JSONObject.fromObject(returnMap, jsonConfig);
    }


    public static String jsonString(String message, Object data, int status, String[] excludes) {
        Map<String, Object> returnMap = new LinkedHashMap();
        returnMap.put("status", status);
        returnMap.put("data", data);
        returnMap.put("message", message);
        returnMap.put("timestamp", System.currentTimeMillis() / 1000);
        JsonConfig jsonConfig = new JsonConfig();

        // 排除,避免循环引用 There is a cycle in the hierarchy!
        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        jsonConfig.setIgnoreDefaultExcludes(true);
        jsonConfig.setAllowNonStringKeys(true);

        if (excludes != null && excludes.length > 0) {
            jsonConfig.setExcludes(excludes);
        }

        JSONObject json = JSONObject.fromObject(returnMap);
        return json.toString();
    }

    public static String jsonString(String message, int status) {
        Map<String, Object> returnMap = new LinkedHashMap();
        returnMap.put("status", status);
        returnMap.put("message", message);
        returnMap.put("timestamp", System.currentTimeMillis() / 1000);
        JSONObject json = JSONObject.fromObject(returnMap);
        return json.toString();
    }

    public static String jsonStringError(String message, int status) {
        Map<String, Object> returnMap = new LinkedHashMap();
        returnMap.put("status", status);
        returnMap.put("message", message);
        returnMap.put("timestamp", System.currentTimeMillis() / 1000);
        JSONObject json = JSONObject.fromObject(returnMap);
        return json.toString();
    }
}
